var doctorChargePrice = {
    id: "doctorChargePriceTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

doctorChargePrice.initColumn = function () {// 初始化表格的列
    var columns = [
        {field: 'selectItem', radio: true},
        {title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle'},
        {title: '患者姓名', field: 'customerName', align: 'center', valign: 'middle', sortable: true, width: 100},
        {title: '电话', field: 'customerPhone', align: 'center', valign: 'middle', sortable: true, width: 140},
        {
            title: '性别', field: 'customerSex', align: 'center', valign: 'middle', sortable: true, width: 100,
            formatter: function (value, row, index) {
                if (value == 1) {
                    return "男";
                } else if (value == 2) {
                    return "女"
                } else {
                    return "未知"
                }
            }
        },
        {title: '医生', field: 'doctor', align: 'center', valign: 'middle', sortable: true, width: 100},
        {title: '项目名称', field: 'payServiceName', align: 'center', valign: 'middle', sortable: true, width: 400},
        {
            title: '划价状态', field: 'executeStatus', align: 'center', valign: 'middle', sortable: true, width: 100,
            formatter: function (value, row, index) {
                if (value == 1) {
                    return "<span style='color: green'>划价结束</span>";
                } else if (value == 0) {
                    return "划价中"
                }
            }
        },
        {title: '划价时间', field: 'createTime', align: 'center', valign: 'middle', sortable: true, width: 100},
    ];
    return columns;
};

doctorChargePrice.search = function () {
    doctorChargePrice.table.refresh({query: doctorChargePrice.formParams()});
    doctorChargePrice.total();
};

doctorChargePrice.resetSearch = function () {
    $("#startTime").val("");
    $("#endTime").val("");
    $("#executeStatus").val("请选择");
    $("#doctor").val("");
    doctorChargePrice.inquire()
};

doctorChargePrice.inquire = function () {
    doctorChargePrice.search();
};

doctorChargePrice.close = function () {
    parent.layer.close(parent.layer.getFrameIndex(window.name));
};

doctorChargePrice.formParams = function () {// 查询表单提交参数对象  @returns {{}}
    var queryData = {};
    queryData['reach'] = $("#doctorInputPublic", parent.document).val();
    queryData['startTime'] = $("#startTime").val();
    queryData['endTime'] = $("#endTime").val();
    queryData['executeStatus'] = $("#executeStatus").val();
    queryData['doctor'] = $("#doctor").val();
    return queryData;
};

var parseParam = function (param) {//把对象拼接成URL的键值对参数param1=value1&param2=value2
    var paramStr = "";
    $.each(param, function (i) {
        paramStr += '&' + getSingleData(param, i);
    });
    return paramStr.substr(1);
};

$("#doctorSearchAll", parent.document).click(function () {//主页搜索
    doctorChargePrice.search();
});

var getSingleData = function (param, key) {
    return key + "=" + param[key];
};

doctorChargePrice.total = function () {//统计
    var reach = $("#doctorInputPublic", parent.document).val();
    var startTime = $("#startTime").val();  //回访时间
    var endTime = $("#endTime").val();  //回访时间
    var executeStatus = $("#executeStatus").val();  //回访时间.
    var doctor = $("#doctor").val();  //回访时间
    $.ajax({
        type: "GET",
        url: Feng.ctxPath + "/doctorLooked/alltotal",
        data: {
            reach: reach,
            startTime: startTime,
            endTime: endTime,
            executeStatus: executeStatus,
            doctor: doctor,
        },
        dataType: "json",
        success: function (data) {
            $("#all").html(data[0].countNum);//总人数
        }
    });
};

doctorChargePrice.export = function () {//导出数据
    var queryData = {};
    queryData['reach'] = $("#doctorInputPublic", parent.document).val();
    queryData['startTime'] = $("#startTime").val();
    queryData['endTime'] = $("#endTime").val();
    queryData['executeStatus'] = $("#executeStatus").val();
    queryData['doctor'] = $("#doctor").val();
    var param = parseParam(queryData);
    location.href = Feng.ctxPath + '/doctorLooked/doctorChargeExport?' + param;
};

var myDate = new Date();
var month;
var day;
if ((myDate.getMonth() + 1) < 10) {
    month = "0" + (myDate.getMonth() + 1);
} else {
    month = "" + (myDate.getMonth() + 1);
}
if (myDate.getDate() < 10) {
    day = "0" + myDate.getDate();
} else {
    day = "" + myDate.getDate();
}

$(function () {
    var seleteDoctorValue = $('#doctorValue').val();
    if (seleteDoctorValue != "") {
        $("#doctor option[value='" + seleteDoctorValue + "']").attr("selected", "selected");
    }
    document.getElementById("startTime").value = myDate.getFullYear() + "-" + month + "-" + day + " " + "00:00:00";
    document.getElementById("endTime").value = myDate.getFullYear() + "-" + month + "-" + day + " " + "23:59:59";
    var defaultColunms = doctorChargePrice.initColumn();
    var table = new BSTable(doctorChargePrice.id, "/doctorLooked/getDoctorChargePrice", defaultColunms);
    table.setPaginationType("server");
    table.setQueryParams(doctorChargePrice.formParams());
    doctorChargePrice.table = table.init();
    doctorChargePrice.search();
    var startTimeTwo = $("#startTime").val();
    var endTimeTwo = $("#endTime").val();
    setInterval(//采用定时器监听 时间内容是否变化
        function () {//定时执行方法内容
            if ($("#startTime").val() != startTimeTwo || $("#endTime").val() != endTimeTwo) {//如果时间发生变化，就触发查询事件
                doctorChargePrice.search();
                startTimeTwo = $("#startTime").val();//如果值发生变化之后，把最新的值赋值到老的变量里面
                endTimeTwo = $("#endTime").val();
            }
        }, 500);
    $("#zoom").click(function () {
        if ($("#zoom > span").hasClass('tabMark')) {
            $("#zoom > span").removeClass('tabMark').html('条件展开');
            $("#zoom > img,.divFlex").toggle();
            $(document).ready(function () {
                $('#doctorChargePriceTable').bootstrapTable('resetView', {height: $(window).height() - 76});
            });
        } else {
            $("#zoom > span").addClass('tabMark').html('条件收起');
            $("#zoom > img,.divFlex").toggle();
            $(document).ready(function () {
                $('#doctorChargePriceTable').bootstrapTable('resetView', {height: $(window).height() - 137});
            });
        }
    });
});